home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / bash-1.08 / parse.y < prev    next >
Encoding:
Text File  |  1991-08-22  |  64.6 KB  |  2,553 lines

  1. /* Yacc grammar for bash. */
  2.  
  3. /*  Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file LICENSE.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. %{
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include "shell.h"
  25. #include "flags.h"
  26.  
  27. #if defined (READLINE)
  28. #include <readline/readline.h>
  29. #endif /* READLINE */
  30.  
  31. #include <readline/history.h>
  32.  
  33. #define YYDEBUG 1
  34. extern int eof_encountered;
  35. extern int no_line_editing;
  36. extern int interactive, interactive_shell;
  37.  
  38. /* **************************************************************** */
  39. /*                                    */
  40. /*            "Forward" declarations                */
  41. /*                                    */
  42. /* **************************************************************** */
  43.  
  44.  
  45. /* This is kind of sickening.  In order to let these variables be seen by
  46.    all the functions that need them, I am forced to place their declarations
  47.    far away from the place where they should logically be found. */
  48.  
  49. /* prompt_string_pointer points to one of these, never to an actual string. */
  50. char *ps1_prompt, *ps2_prompt;
  51.  
  52. /* Handle on the current prompt string.  Indirectly points through
  53.    ps1_ or ps2_prompt. */
  54. char **prompt_string_pointer = (char **)NULL;
  55. char *current_prompt_string;
  56.  
  57. /* Variables to manage the task of reading here documents, because we need to
  58.    defer the reading until after a complete command has been collected. */
  59. REDIRECT *redirection_needing_here_doc = (REDIRECT *)NULL;
  60. int need_here_doc = 0;
  61. %}
  62.  
  63. %union {
  64.   WORD_DESC *word;        /* the word that we read. */
  65.   int number;            /* the number that we read. */
  66.   WORD_LIST *word_list;
  67.   COMMAND *command;
  68.   REDIRECT *redirect;
  69.   ELEMENT element;
  70.   PATTERN_LIST *pattern;
  71. }
  72.  
  73. /* Reserved words.  Members of the first group are only recognized
  74.    in the case that they are preceded by a list_terminator.  Members
  75.    of the second group are recognized only under special circumstances. */
  76. %token IF THEN ELSE ELIF FI CASE ESAC FOR WHILE UNTIL DO DONE FUNCTION
  77. %token IN BANG
  78.  
  79. /* More general tokens. yylex () knows how to make these. */
  80. %token <word> WORD
  81. %token <number> NUMBER
  82. %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
  83. %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
  84. %token GREATER_BAR
  85.  
  86. /* The types that the various syntactical units return. */
  87.  
  88. %type <command> inputunit command list list0 list1 simple_list simple_list1 simple_command shell_command group_command
  89. %type <command> elif_clause pipeline
  90. %type <redirect> redirection redirections
  91. %type <element> simple_command_element
  92. %type <word_list> words pattern 
  93. %type <pattern> pattern_list case_clause_sequence case_clause_1 pattern_list_1
  94.  
  95. %start inputunit
  96.  
  97. %left '&' ';' '\n' yacc_EOF
  98. %left AND_AND OR_OR
  99. %left '|'
  100. %%
  101.  
  102. inputunit:    simple_list '\n'
  103.             {
  104.               /* Case of regular command.  Discard the error
  105.                  safety net,and return the command just parsed. */
  106.               global_command = $1;
  107.               eof_encountered = 0;
  108.               discard_parser_constructs (0);
  109.               YYACCEPT;
  110.             }
  111.     |    '\n'
  112.             {
  113.               /* Case of regular command, but not a very
  114.                  interesting one.  Return a NULL command. */
  115.               global_command = (COMMAND *)NULL;
  116.               YYACCEPT;
  117.             }
  118.     |
  119.         error '\n'
  120.             {
  121.               /* Error during parsing.  Return NULL command. */
  122.               global_command = (COMMAND *)NULL;
  123.               eof_encountered = 0;
  124.               discard_parser_constructs (1);
  125.               if (interactive)
  126.                 {
  127.                   YYACCEPT;
  128.                 }
  129.               else
  130.                 {
  131.                   YYABORT;
  132.                 }
  133.             }
  134.     |    yacc_EOF
  135.             {
  136.               /* Case of EOF seen by itself.  Do ignoreeof or 
  137.                  not. */
  138.               global_command = (COMMAND *)NULL;
  139.               handle_eof_input_unit ();
  140.               YYACCEPT;
  141.             }
  142.     ;
  143.  
  144. words:    
  145.             { $$ = (WORD_LIST *)NULL; }
  146.     |    words WORD
  147.             { $$ = make_word_list ($2, $1); }
  148.     ;
  149.  
  150. redirection:    '>' WORD
  151.             { $$ = make_redirection ( 1, r_output_direction, $2); }
  152.     |    '<' WORD
  153.             { $$ = make_redirection ( 0, r_input_direction, $2); }
  154.     |    NUMBER '>' WORD
  155.             { $$ = make_redirection ($1, r_output_direction, $3); }
  156.     |    NUMBER '<' WORD
  157.             { $$ = make_redirection ($1, r_input_direction, $3); }
  158.     |    GREATER_GREATER WORD
  159.             { $$ = make_redirection ( 1, r_appending_to, $2); }
  160.     |    NUMBER GREATER_GREATER WORD
  161.             { $$ = make_redirection ($1, r_appending_to, $3); }
  162.     |    LESS_LESS WORD
  163.             {
  164.               $$ = make_redirection ( 0, r_reading_until, $2);
  165.               redirection_needing_here_doc = $$;
  166.               need_here_doc = 1;
  167.             }
  168.     |    NUMBER LESS_LESS WORD
  169.             {
  170.               $$ = make_redirection ($1, r_reading_until, $3);
  171.               redirection_needing_here_doc = $$;
  172.               need_here_doc = 1;
  173.             }
  174.     |    LESS_AND NUMBER
  175.             { $$ = make_redirection ( 0, r_duplicating, $2); }
  176.     |    NUMBER LESS_AND NUMBER
  177.             { $$ = make_redirection ($1, r_duplicating, $3); }
  178.     |    GREATER_AND NUMBER
  179.             { $$ = make_redirection ( 1, r_duplicating, $2); }
  180.     |    NUMBER GREATER_AND NUMBER
  181.             { $$ = make_redirection ($1, r_duplicating, $3); }
  182.     |    LESS_LESS_MINUS WORD
  183.             {
  184.               $$ = make_redirection ( 0, r_deblank_reading_until, $2);
  185.               redirection_needing_here_doc = $$;
  186.               need_here_doc = 1;
  187.             }
  188.     |    NUMBER LESS_LESS_MINUS WORD
  189.             {
  190.               $$ = make_redirection ($1, r_deblank_reading_until, $3);
  191.               redirection_needing_here_doc = $$;
  192.               need_here_doc = 1;
  193.             }
  194.     |    GREATER_AND '-'
  195.             { $$ = make_redirection ( 1, r_close_this, 0); }
  196.     |    NUMBER GREATER_AND '-'
  197.             { $$ = make_redirection ($1, r_close_this, 0); }
  198.     |    LESS_AND '-'
  199.             { $$ = make_redirection ( 0, r_close_this, 0); }
  200.     |    NUMBER LESS_AND '-'
  201.             { $$ = make_redirection ($1, r_close_this, 0); }
  202.     |    AND_GREATER WORD
  203.             { $$ = make_redirection ( 1, r_err_and_out, $2); }
  204.     |    GREATER_AND WORD
  205.             { $$ = make_redirection ( 1, r_err_and_out, $2); }
  206.     |    NUMBER LESS_GREATER WORD
  207.             { $$ = make_redirection ( $1, r_input_output, $3); }
  208.     |    LESS_GREATER WORD
  209.             {
  210.               REDIRECT *t1, *t2;
  211.               extern WORD_DESC *copy_word ();
  212.  
  213.               t1 = make_redirection ( 0, r_input_direction, $2);
  214.               t2 = make_redirection ( 1, r_output_direction, copy_word ($2));
  215.               t1->next = t2;
  216.               $$ = t1;
  217.             }              
  218.     |    GREATER_BAR WORD
  219.             { $$ = make_redirection ( 1, r_output_force, $2); }
  220.     |    NUMBER GREATER_BAR WORD
  221.             { $$ = make_redirection ( $1, r_output_force, $3); }
  222.     ;
  223.  
  224. simple_command_element: WORD
  225.             { $$.word = $1; $$.redirect = 0; }
  226.     |    redirection
  227.             { $$.redirect = $1; $$.word = 0; }
  228.     ;
  229.  
  230. redirections:    redirection
  231.             {
  232.               $$ = $1;
  233.             }
  234.     |    redirections redirection
  235.             { 
  236.               register REDIRECT *t = $1;
  237.  
  238.               while (t->next)
  239.                 t = t->next;
  240.               t->next = $2; 
  241.               $$ = $1;
  242.             }
  243.     ;
  244.  
  245. simple_command:    simple_command_element
  246.             { $$ = make_simple_command ($1, (COMMAND *)NULL); }
  247.     |    simple_command simple_command_element
  248.             { $$ = make_simple_command ($2, $1); }
  249.     ;
  250.  
  251. command:    simple_command
  252.             { $$ = clean_simple_command ($1); }
  253.  
  254.     |    shell_command
  255.             { $$ = $1; }
  256.  
  257.     |    shell_command redirections
  258.             {
  259.               $$->redirects = $2;
  260.               $$ = $1;
  261.             }
  262.     ;
  263.  
  264. shell_command:    FOR WORD newlines DO list DONE
  265.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5); }
  266.     |    FOR WORD newlines '{' list '}'
  267.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("$@", (WORD_LIST *)NULL), $5); }
  268.     |    FOR WORD ';' newlines DO list DONE
  269.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
  270.     |    FOR WORD ';' newlines '{' list '}'
  271.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
  272.  
  273.     |    FOR WORD newlines IN words list_terminator newlines DO list DONE
  274.             { $$ = make_for_command ($2, (WORD_LIST *)reverse_list ($5), $9); }
  275.     |    FOR WORD newlines IN words list_terminator newlines '{' list '}'
  276.             { $$ = make_for_command ($2, (WORD_LIST *)reverse_list ($5), $9); }
  277.  
  278.     |    CASE WORD newlines IN newlines ESAC
  279.             { $$ = make_case_command ($2, (PATTERN_LIST *)NULL); }
  280.     |    CASE WORD newlines IN case_clause_sequence newlines ESAC
  281.             { $$ = make_case_command ($2, $5); }
  282.     |    CASE WORD newlines IN case_clause_1 ESAC
  283.             { /* Nobody likes this...
  284.                  report_syntax_error ("Inserted `;;'"); */
  285.               $$ = make_case_command ($2, $5); }
  286.  
  287.     |    IF list THEN list FI
  288.             { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
  289.     |    IF list THEN list ELSE list FI
  290.             { $$ = make_if_command ($2, $4, $6); }
  291.     |    IF list THEN list elif_clause FI
  292.             { $$ = make_if_command ($2, $4, $5); }
  293.  
  294.     |    WHILE list DO list DONE
  295.             { $$ = make_while_command ($2, $4); }
  296.     |    UNTIL list DO list DONE
  297.             { $$ = make_until_command ($2, $4); }
  298.  
  299.     |    '(' list ')'
  300.             { $2->subshell = WANT_SUBSHELL; $$ = $2; }
  301.  
  302.     |    group_command
  303.             { $$ = $1; }
  304.  
  305.     |    WORD '(' ')' newlines group_command
  306.             { $$ = make_function_def ($1, $5); }
  307.  
  308.     |    FUNCTION WORD '(' ')' newlines group_command
  309.             { $$ = make_function_def ($2, $6); }
  310.  
  311.     |    FUNCTION WORD newlines group_command
  312.             { $$ = make_function_def ($2, $4); }
  313.     ;
  314.  
  315. group_command:    '{' list '}'
  316.             { $$ = make_group_command ($2); }
  317.     ;
  318.  
  319. elif_clause:    ELIF list THEN list
  320.             { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
  321.     |    ELIF list THEN list ELSE list
  322.             { $$ = make_if_command ($2, $4, $6); }
  323.     |    ELIF list THEN list elif_clause
  324.             { $$ = make_if_command ($2, $4, $5); }
  325.     ;
  326.  
  327.  
  328. case_clause_1:    pattern_list_1
  329.     |    case_clause_sequence pattern_list_1
  330.             { $2->next = $1; $$ = $2; }
  331.     ;
  332.  
  333. pattern_list_1:    newlines pattern ')' list
  334.             { $$ = make_pattern_list ($2, $4); }
  335.     |    newlines pattern ')' newlines
  336.             { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
  337.     ;
  338.  
  339. case_clause_sequence:  pattern_list
  340.  
  341.     |    case_clause_sequence pattern_list
  342.             { $2->next = $1; $$ = $2; }
  343.     ;
  344.  
  345. pattern_list:    newlines pattern ')' list SEMI_SEMI
  346.             { $$ = make_pattern_list ($2, $4); }
  347.     |    newlines pattern ')' newlines SEMI_SEMI
  348.             { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
  349.     ;
  350.  
  351. pattern:    WORD
  352.             { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
  353.     |    pattern '|' WORD
  354.             { $$ = make_word_list ($3, $1); }
  355.     ;
  356.  
  357. /* A list allows leading or trailing newlines and
  358.    newlines as operators (equivalent to semicolons).
  359.    It must end with a newline or semicolon.
  360.    Lists are used within commands such as if, for, while.  */
  361.  
  362. list:        newlines list0
  363.             {
  364.               $$ = $2;
  365.               if (need_here_doc)
  366.                 make_here_document (redirection_needing_here_doc);
  367.               need_here_doc = 0;
  368.              }
  369.     ;
  370.  
  371. list0:        list1
  372.     |    list1 '\n' newlines
  373.     |    list1 '&' newlines
  374.             { $$ = command_connect ($1, 0, '&'); }
  375.     |    list1 ';' newlines
  376.  
  377.     ;
  378.  
  379. list1:        list1 AND_AND newlines list1
  380.             { $$ = command_connect ($1, $4, AND_AND); }
  381.     |    list1 OR_OR newlines list1
  382.             { $$ = command_connect ($1, $4, OR_OR); }
  383.     |    list1 '&' newlines list1
  384.             { $$ = command_connect ($1, $4, '&'); }
  385.     |    list1 ';' newlines list1
  386.             { $$ = command_connect ($1, $4, ';'); }
  387.     |    list1 '\n' newlines list1
  388.             { $$ = command_connect ($1, $4, ';'); }
  389.     |    pipeline
  390.             { $$ = $1; }
  391.     |    BANG pipeline
  392.             {
  393.               $2->invert_pipeline = 1;
  394.               $$ = $2;
  395.             }
  396.     ;
  397.  
  398. list_terminator:'\n'
  399.     |    ';'
  400.     |    yacc_EOF
  401.     ;
  402.  
  403. newlines:
  404.     |    newlines '\n'
  405.     ;
  406.  
  407. /* A simple_list is a list that contains no significant newlines
  408.    and no leading or trailing newlines.  Newlines are allowed
  409.    only following operators, where they are not significant.
  410.  
  411.    This is what an inputunit consists of.  */
  412.  
  413. simple_list:    simple_list1
  414.             {
  415.               $$ = $1;
  416.               if (need_here_doc)
  417.                 make_here_document (redirection_needing_here_doc);
  418.               need_here_doc = 0;
  419.             }
  420.     |    simple_list1 '&'
  421.             {
  422.               $$ = command_connect ($1, (COMMAND *)NULL, '&');
  423.               if (need_here_doc)
  424.                 make_here_document (redirection_needing_here_doc);
  425.               need_here_doc = 0;
  426.             }
  427.     |    simple_list1 ';'
  428.             {
  429.               $$ = $1;
  430.               if (need_here_doc)
  431.                 make_here_document (redirection_needing_here_doc);
  432.               need_here_doc = 0;
  433.             }
  434.     ;
  435.  
  436. simple_list1:    simple_list1 AND_AND newlines simple_list1
  437.             { $$ = command_connect ($1, $4, AND_AND); }
  438.     |    simple_list1 OR_OR newlines simple_list1
  439.             { $$ = command_connect ($1, $4, OR_OR); }
  440.     |    simple_list1 '&' simple_list1
  441.             { $$ = command_connect ($1, $3, '&'); }
  442.     |    simple_list1 ';' simple_list1
  443.             { $$ = command_connect ($1, $3, ';'); }
  444.     |    pipeline
  445.             { $$ = $1; }
  446.     |    BANG pipeline
  447.             {
  448.               $2->invert_pipeline = 1;
  449.               $$ = $2;
  450.             }
  451.     ;
  452.  
  453. pipeline:
  454.         pipeline '|' newlines pipeline
  455.             { $$ = command_connect ($1, $4, '|'); }
  456.     |    command
  457.             { $$ = $1; }
  458.     ;
  459. %%
  460.  
  461. /* Initial size to allocate for tokens, and the
  462.    amount to grow them by. */
  463. #define TOKEN_DEFAULT_GROW_SIZE 512
  464.  
  465. /* The token currently being read. */
  466. int current_token = 0;
  467.  
  468. /* The last read token, or NULL.  read_token () uses this for context
  469.    checking. */
  470. int last_read_token = 0;
  471.  
  472. /* The token read prior to last_read_token. */
  473. int token_before_that = 0;
  474.  
  475. /* Global var is non-zero when end of file has been reached. */
  476. int EOF_Reached = 0;
  477.  
  478. /* yy_getc () returns the next available character from input or EOF.
  479.    yy_ungetc (c) makes `c' the next character to read.
  480.    init_yy_io (get, unget), makes the function `get' the installed function
  481.    for getting the next character, and makes `unget' the installed function
  482.    for un-getting a character. */
  483. return_EOF ()            /* does nothing good. */
  484. {
  485.   return (EOF);
  486. }
  487.  
  488. /* Variables containing the current get and unget functions. */
  489.  
  490. /* Some stream `types'. */
  491. #define st_stream 0
  492. #define st_string 1
  493.  
  494. Function *get_yy_char = return_EOF;
  495. Function *unget_yy_char = return_EOF;
  496. int yy_input_type = st_stream;
  497. FILE *yy_input_dev = (FILE *)NULL;
  498.  
  499. /* The current stream name.  In the case of a file, this is a filename. */
  500. char *stream_name = (char *)NULL;
  501.  
  502. /* Function to set get_yy_char and unget_yy_char. */
  503. init_yy_io (get_function, unget_function, type, location)
  504.      Function *get_function, *unget_function;
  505.      int type;
  506.      FILE *location;
  507. {
  508.   get_yy_char = get_function;
  509.   unget_yy_char = unget_function;
  510.   yy_input_type = type;
  511.   yy_input_dev = location;
  512. }
  513.  
  514. /* Call this to get the next character of input. */
  515. yy_getc ()
  516. {
  517.   return (*get_yy_char) ();
  518. }
  519.  
  520. /* Call this to unget C.  That is, to make C the next character
  521.    to be read. */
  522. yy_ungetc (c)
  523. {
  524.   return (*unget_yy_char) (c);
  525. }
  526.  
  527. /* **************************************************************** */
  528. /*                                    */
  529. /*          Let input be read from readline ().            */
  530. /*                                    */
  531. /* **************************************************************** */
  532.  
  533. #if defined (READLINE)
  534. char *current_readline_prompt = (char *)NULL;
  535. char *current_readline_line = (char *)NULL;
  536. int current_readline_line_index = 0;
  537.  
  538. static int readline_initialized_yet = 0;
  539. int
  540. yy_readline_get ()
  541. {
  542.   if (!current_readline_line)
  543.     {
  544.       char *readline ();
  545.       SigHandler *old_sigint;
  546.       extern sighandler sigint_sighandler ();
  547.       extern int interrupt_immediately;
  548.  
  549. #if defined (JOB_CONTROL)
  550.       extern int shell_pgrp, job_control;
  551. #endif
  552.  
  553.       if (!readline_initialized_yet)
  554.     {
  555.       initialize_readline ();
  556.       readline_initialized_yet = 1;
  557.     }
  558.  
  559. #if defined (JOB_CONTROL)
  560.       if (job_control)
  561.     give_terminal_to (shell_pgrp);
  562. #endif /* JOB_CONTROL */
  563.  
  564.       old_sigint = (SigHandler *)signal (SIGINT, sigint_sighandler);
  565.       interrupt_immediately++;
  566.  
  567.       if (!current_readline_prompt)
  568.     current_readline_line = readline ("");
  569.       else
  570.     current_readline_line = readline (current_readline_prompt);
  571.  
  572.       interrupt_immediately--;
  573.       signal (SIGINT, old_sigint);
  574.  
  575.       /* Reset the prompt to whatever is in the decoded value of
  576.      prompt_string_pointer. */
  577.       reset_readline_prompt ();
  578.  
  579.       current_readline_line_index = 0;
  580.  
  581.       if (!current_readline_line)
  582.     {
  583.       current_readline_line_index = 0;
  584.       return (EOF);
  585.     }
  586.  
  587.       current_readline_line =
  588.     (char *)xrealloc (current_readline_line,
  589.               2 + strlen (current_readline_line));
  590.       strcat (current_readline_line, "\n");
  591.     }
  592.  
  593.   if (!current_readline_line[current_readline_line_index])
  594.     {
  595.       free (current_readline_line);
  596.       current_readline_line = (char *)NULL;
  597.       return (yy_readline_get ());
  598.     }
  599.   else
  600.     {
  601.       int c = current_readline_line[current_readline_line_index++];
  602.       return (c);
  603.     }
  604. }
  605.  
  606. int
  607. yy_readline_unget (c)
  608. {
  609.   if (current_readline_line_index && current_readline_line)
  610.     current_readline_line[--current_readline_line_index] = c;
  611.   return (c);
  612. }
  613.   
  614. with_input_from_stdin ()
  615. {
  616.   init_yy_io (yy_readline_get, yy_readline_unget,
  617.           st_string, (FILE *)current_readline_line);
  618.   stream_name = savestring ("readline stdin");
  619. }
  620.  
  621. #else  /* !READLINE */
  622.  
  623. with_input_from_stdin ()
  624. {
  625.   with_input_from_stream (stdin, "stdin");
  626. }
  627. #endif    /* !READLINE */
  628.  
  629. /* **************************************************************** */
  630. /*                                    */
  631. /*   Let input come from STRING.  STRING is zero terminated.        */
  632. /*                                    */
  633. /* **************************************************************** */
  634.  
  635. int
  636. yy_string_get ()
  637. {
  638.   /* If the string doesn't exist, or is empty, EOF found. */
  639.   if (!(char *)yy_input_dev || !*(char *)yy_input_dev)
  640.     return (EOF);
  641.   else
  642.     {
  643.       register char *temp = (char *)yy_input_dev;
  644.       int c = *temp++;
  645.       yy_input_dev = (FILE *)temp;
  646.       return (c);
  647.     }
  648. }
  649.  
  650. int
  651. yy_string_unget (c)
  652.      int c;
  653. {
  654.   register char *temp = (char *)yy_input_dev;
  655.   *(--temp) = c;
  656.   yy_input_dev = (FILE *)temp;
  657.   return (c);
  658. }
  659.  
  660. with_input_from_string (string, name)
  661.      char *string;
  662.      char *name;
  663. {
  664.   init_yy_io (yy_string_get, yy_string_unget, st_string, (FILE *)string);
  665.   stream_name = savestring (name);
  666. }
  667.  
  668. /* **************************************************************** */
  669. /*                                    */
  670. /*             Let input come from STREAM.            */
  671. /*                                    */
  672. /* **************************************************************** */
  673.  
  674. int
  675. yy_stream_get ()
  676. {
  677.   if (yy_input_dev)
  678. #if defined (USG)
  679.     return (sysv_getc (yy_input_dev));
  680. #else
  681.     return (getc (yy_input_dev));
  682. #endif    /* USG */
  683.   else return (EOF);
  684. }
  685.  
  686. int
  687. yy_stream_unget (c)
  688.      int c;
  689. {
  690.   return (ungetc (c, yy_input_dev));
  691. }
  692.  
  693. with_input_from_stream (stream, name)
  694.      FILE *stream;
  695.      char *name;
  696. {
  697.   init_yy_io (yy_stream_get, yy_stream_unget, st_stream, stream);
  698.   stream_name = savestring (name);
  699. }
  700.  
  701. typedef struct stream_saver {
  702.   struct stream_saver *next;
  703.   Function *getter, *putter;
  704.   int type, line;
  705.   char *location, *name;
  706. } STREAM_SAVER;
  707.  
  708. /* The globally known line number. */
  709. int line_number = 0;
  710.  
  711. STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
  712.  
  713. push_stream ()
  714. {
  715.   STREAM_SAVER *temp = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
  716.   temp->type = yy_input_type;
  717.   temp->location = (char *)yy_input_dev;
  718.   temp->getter = get_yy_char;
  719.   temp->putter = unget_yy_char;
  720.   temp->line = line_number;
  721.   temp->name = stream_name; stream_name = (char *)NULL;
  722.   temp->next = stream_list;
  723.   stream_list = temp;
  724.   EOF_Reached = line_number = 0;
  725. }
  726.  
  727. pop_stream ()
  728. {
  729.   if (!stream_list)
  730.     {
  731.       EOF_Reached = 1;
  732.     }
  733.   else
  734.     {
  735.       STREAM_SAVER *temp = stream_list;
  736.     
  737.       EOF_Reached = 0;
  738.       stream_list = stream_list->next;
  739.  
  740.       if (stream_name)
  741.     free (stream_name);
  742.       stream_name = temp->name;
  743.  
  744.       init_yy_io (temp->getter, temp->putter, temp->type, (FILE *)temp->location);
  745.       line_number = temp->line;
  746.       free (temp);
  747.     }
  748. }
  749.  
  750. /*
  751.  * This is used to inhibit alias expansion and reserved word recognition
  752.  * inside case statement pattern lists.  A `case statement pattern list'
  753.  * is:
  754.  *    everything between the `in' in a `case word in' and the next ')'
  755.  *    or `esac'
  756.  *    everything between a `;;' and the next `)' or `esac'
  757.  */
  758. static int in_case_pattern_list = 0;
  759.  
  760. #if defined (ALIAS)
  761. /*
  762.  * Pseudo-global variables used in implementing token-wise alias expansion.
  763.  */
  764.  
  765. static int expand_next_token = 0;
  766. static char *current_token_being_expanded = (char *)NULL;
  767. static char *pending_token_being_expanded = (char *)NULL;
  768.  
  769. /*
  770.  * Pushing and popping strings.  This works together with shell_getc to 
  771.  * implement alias expansion on a per-token basis.
  772.  */
  773.  
  774. typedef struct string_saver {
  775.   struct string_saver *next;
  776.   int expand_alias;  /* value to set expand_alias to when string is popped */
  777.   char *saved_line;
  778.   int saved_line_size, saved_line_index, saved_line_terminator;
  779.   char *saved_token_being_expanded;
  780. } STRING_SAVER;
  781.  
  782. STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
  783.  
  784. static void save_expansion ();
  785.  
  786. /*
  787.  * Push the current shell_input_line onto a stack of such lines and make S
  788.  * the current input.  Used when expanding aliases.  EXPAND is used to set
  789.  * the value of expand_next_token when the string is popped, so that the
  790.  * word after the alias in the original line is handled correctly when the
  791.  * alias expands to multiple words.  TOKEN is the token that was expanded
  792.  * into S; it is saved and used to prevent infinite recursive expansion.
  793.  */
  794. static void
  795. push_string (s, expand, token)
  796.      char *s;
  797.      int expand;
  798.      char *token;
  799. {
  800.   extern char *shell_input_line;
  801.   extern int shell_input_line_size, shell_input_line_index,
  802.          shell_input_line_terminator;
  803.   STRING_SAVER *temp = (STRING_SAVER *) xmalloc (sizeof (STRING_SAVER));
  804.  
  805.   temp->expand_alias = expand;
  806.   temp->saved_line = shell_input_line;
  807.   temp->saved_line_size = shell_input_line_size;
  808.   temp->saved_line_index = shell_input_line_index;
  809.   temp->saved_line_terminator = shell_input_line_terminator;
  810.   temp->saved_token_being_expanded = current_token_being_expanded;
  811.   temp->next = pushed_string_list;
  812.   pushed_string_list = temp;
  813.  
  814.   save_expansion (token);
  815.  
  816.   current_token_being_expanded = token;
  817.   shell_input_line = s;
  818.   shell_input_line_size = strlen (s);
  819.   shell_input_line_index = 0;
  820.   shell_input_line_terminator = '\0';
  821.   expand_next_token = 0;
  822. }
  823.  
  824. /*
  825.  * Make the top of the pushed_string stack be the current shell input.
  826.  * Only called when there is something on the stack.  Called from shell_getc
  827.  * when it thinks it has consumed the string generated by an alias expansion
  828.  * and needs to return to the original input line.
  829.  */
  830. static void
  831. pop_string ()
  832. {
  833.   extern char *shell_input_line;
  834.   extern int shell_input_line_size, shell_input_line_index,
  835.          shell_input_line_terminator;
  836.   STRING_SAVER *t;
  837.  
  838.   if (shell_input_line)
  839.     free (shell_input_line);
  840.   shell_input_line = pushed_string_list->saved_line;
  841.   shell_input_line_index = pushed_string_list->saved_line_index;
  842.   shell_input_line_size = pushed_string_list->saved_line_size;
  843.   shell_input_line_terminator = pushed_string_list->saved_line_terminator;
  844.   expand_next_token = pushed_string_list->expand_alias;
  845.   pending_token_being_expanded = pushed_string_list->saved_token_being_expanded;
  846.   t = pushed_string_list;
  847.   pushed_string_list = pushed_string_list->next;
  848.   free((char *)t);
  849. }
  850.  
  851. static void
  852. free_string_list ()
  853. {
  854.   register STRING_SAVER *t = pushed_string_list, *t1;
  855.  
  856.   while (t)
  857.     {
  858.       t1 = t->next;
  859.       if (t->saved_line)
  860.     free (t->saved_line);
  861.       if (t->saved_token_being_expanded)
  862.     free (t->saved_token_being_expanded);
  863.       free ((char *)t);
  864.       t = t1;
  865.     }
  866.   pushed_string_list = (STRING_SAVER *)NULL;
  867. }
  868.  
  869. /* This is a stack to save the values of all tokens for which alias
  870.    expansion has been performed during the current call to read_token ().
  871.    It is used to prevent alias expansion loops:
  872.  
  873.       alias foo=bar
  874.       alias bar=baz
  875.       alias baz=foo
  876.  
  877.    Ideally this would be taken care of by push and pop string, but because
  878.    of when strings are popped the stack will not contain the correct
  879.    strings to test against.  (The popping is done in shell_getc, so that when
  880.    the current string is exhausted, shell_getc can simply pop that string off
  881.    the stack, restore the previous string, and continue with the character
  882.    following the token whose expansion was originally pushed on the stack.)
  883.  
  884.    What we really want is a record of all tokens that have been expanded for
  885.    aliases during the `current' call to read_token().  This does that, at the
  886.    cost of being somewhat special-purpose (OK, OK vile and unclean).  Brian,
  887.    you had better rewrite this whole piece of garbage before the next version
  888.    is released.
  889. */
  890.  
  891. typedef struct _exp_saver {
  892.       struct _exp_saver *next;
  893.       char *saved_token;
  894. } EXPANSION_SAVER;
  895.  
  896. EXPANSION_SAVER *expanded_token_stack = (EXPANSION_SAVER *)NULL;
  897.  
  898. static void
  899. save_expansion (s)
  900.      char *s;
  901. {
  902.   EXPANSION_SAVER *t;
  903.  
  904.   t = (EXPANSION_SAVER *) xmalloc (sizeof (EXPANSION_SAVER));
  905.   t->saved_token = savestring (s);
  906.   t->next = expanded_token_stack;
  907.   expanded_token_stack = t;
  908. }
  909.  
  910. /*
  911.  * Return 1 if TOKEN has already been expanded in the current `stack' of
  912.  * expansions.  If it has been expanded already, it will appear as the value
  913.  * of saved_token for some entry in the stack of expansions created for the
  914.  * current token being expanded.
  915.  */
  916. static int
  917. token_has_been_expanded (token)
  918.      char *token;
  919. {
  920.   register EXPANSION_SAVER *t = expanded_token_stack;
  921.  
  922.   while (t)
  923.     {
  924.       if (STREQ (token, t->saved_token))
  925.     return (1);
  926.       t = t->next;
  927.     }
  928.   return (0);
  929. }
  930.  
  931. static void
  932. free_expansion_stack ()
  933. {
  934.   register EXPANSION_SAVER *t = expanded_token_stack, *t1;
  935.  
  936.   while (t)
  937.     {
  938.       t1 = t->next;
  939.       free (t->saved_token);
  940.       free (t);
  941.       t = t1;
  942.     }
  943.   expanded_token_stack = (EXPANSION_SAVER *)NULL;
  944. }
  945.  
  946. #endif /* ALIAS */
  947.  
  948. /* Return a line of text, taken from wherever yylex () reads input.
  949.    If there is no more input, then we return NULL. */
  950. char *
  951. read_a_line ()
  952. {
  953.   char *line_buffer = (char *)NULL;
  954.   int indx = 0, buffer_size = 0;
  955.   int c;
  956.  
  957.   while (1)
  958.     {
  959.       c = yy_getc ();
  960.  
  961.       if (c == 0)
  962.     continue;
  963.  
  964.       /* If there is no more input, then we return NULL. */
  965.       if (c == EOF)
  966.     {
  967.       c = '\n';
  968.       if (!line_buffer)
  969.         return ((char *)NULL);
  970.     }
  971.  
  972.       /* `+2' in case the final (200'th) character in the buffer is a newline;
  973.      otherwise the code below that NULL-terminates it will write over the
  974.      201st slot and kill the range checking in free(). */
  975.       if (indx + 2 > buffer_size)
  976.     if (!buffer_size)
  977.       line_buffer = (char *)xmalloc (buffer_size = 200);
  978.     else
  979.       line_buffer = (char *)xrealloc (line_buffer, buffer_size += 200);
  980.  
  981.       line_buffer[indx++] = c;
  982.       if (c == '\n')
  983.     {
  984.       line_buffer[indx] = '\0';
  985.       return (line_buffer);
  986.     }
  987.     }
  988. }
  989.  
  990. /* Return a line as in read_a_line (), but insure that the prompt is
  991.    the secondary prompt. */
  992. char *
  993. read_secondary_line ()
  994. {
  995.   prompt_string_pointer = &ps2_prompt;
  996.   prompt_again ();
  997.   return (read_a_line ());
  998. }
  999.  
  1000.  
  1001. /* **************************************************************** */
  1002. /*                                    */
  1003. /*                YYLEX ()                */
  1004. /*                                    */
  1005. /* **************************************************************** */
  1006.  
  1007. /* Reserved words.  These are only recognized as the first word of a
  1008.    command.  TOKEN_WORD_ALIST. */
  1009. struct {
  1010.   char *word;
  1011.   int token;
  1012. } token_word_alist[] = {
  1013.   {"if", IF},
  1014.   {"then", THEN},
  1015.   {"else", ELSE},
  1016.   {"elif", ELIF},
  1017.   {"fi", FI},
  1018.   {"case", CASE},
  1019.   {"esac", ESAC},
  1020.   {"for", FOR},
  1021.   {"while", WHILE},
  1022.   {"until", UNTIL},
  1023.   {"do", DO},
  1024.   {"done", DONE},
  1025.   {"in", IN},
  1026.   {"function", FUNCTION},
  1027.   {"{", '{'},
  1028.   {"}", '}'},
  1029.   {"!", BANG},
  1030.   {(char *)NULL, 0}
  1031. };
  1032.  
  1033. /* Where shell input comes from.  History expansion is performed on each
  1034.    line, when the shell is interactive. */
  1035. char *shell_input_line = (char *)NULL;
  1036. int shell_input_line_index = 0;
  1037. int shell_input_line_size = 0;    /* amount allocated for shell_input_line */
  1038. int shell_input_line_len = 0;    /* strlen (shell_input_line) */
  1039.  
  1040. /* Either zero, or EOF. */
  1041. int shell_input_line_terminator = 0;
  1042.  
  1043. /* Return the next shell input character.  This always reads characters
  1044.    from shell_input_line; when that line is exhausted, it is time to
  1045.    read the next line. */
  1046. int
  1047. shell_getc (remove_quoted_newline)
  1048.      int remove_quoted_newline;
  1049. {
  1050.   extern int login_shell;
  1051.   int c;
  1052.  
  1053.   QUIT;    /* XXX this is experimental */
  1054.  
  1055. #if defined (ALIAS)
  1056.   /* If shell_input_line[shell_input_line_index] == 0, but there is
  1057.      something on the pushed list of strings, then we don't want to go
  1058.      off and get another line.  We let the code down below handle it. */
  1059.  
  1060.   if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
  1061.                 (pushed_string_list == (STRING_SAVER *)NULL)))
  1062. #else /* !ALIAS */
  1063.   if (!shell_input_line || !shell_input_line[shell_input_line_index])
  1064. #endif /* !ALIAS */
  1065.     {
  1066.       register int i, l;
  1067.       char *pre_process_line (), *expansions;
  1068.  
  1069.       restart_read_next_line:
  1070.  
  1071.       line_number++;
  1072.  
  1073.     restart_read:
  1074.  
  1075.       QUIT;    /* XXX experimental */
  1076.  
  1077.       i = 0;
  1078.       shell_input_line_terminator = 0;
  1079.  
  1080. #if defined (JOB_CONTROL)
  1081.       notify_and_cleanup ();
  1082. #endif
  1083.  
  1084.       clearerr (stdin);
  1085.       while (c = yy_getc ())
  1086.     {
  1087.       if (i + 2 > shell_input_line_size)
  1088.         shell_input_line = (char *)
  1089.           xrealloc (shell_input_line, shell_input_line_size += 256);
  1090.  
  1091.       if (c == EOF)
  1092.         {
  1093.           clearerr (stdin);
  1094.  
  1095.           if (!i)
  1096.         shell_input_line_terminator = EOF;
  1097.  
  1098.           shell_input_line[i] = '\0';
  1099.           break;
  1100.         }
  1101.  
  1102.       shell_input_line[i++] = c;
  1103.  
  1104.       if (c == '\n')
  1105.         {
  1106.           shell_input_line[--i] = '\0';
  1107.           break;
  1108.         }
  1109.     }
  1110.       shell_input_line_index = 0;
  1111.       shell_input_line_len = i;        /* == strlen (shell_input_line) */
  1112.  
  1113.       if (!shell_input_line[0])
  1114.     goto after_pre_process;
  1115.  
  1116.       if (interactive)
  1117.     {
  1118.       expansions = pre_process_line (shell_input_line, 1, 1);
  1119.  
  1120.       free (shell_input_line);
  1121.       shell_input_line = expansions;
  1122.       shell_input_line_len = shell_input_line ?
  1123.                  strlen (shell_input_line) :
  1124.                  0;
  1125.       /* We have to force the xrealloc below because we don't know the
  1126.          true allocated size of shell_input_line anymore. */
  1127.       shell_input_line_size = shell_input_line_len;
  1128.     }
  1129.  
  1130.   after_pre_process:
  1131.       if (shell_input_line)
  1132.     {
  1133.       if (echo_input_at_read)
  1134.         fprintf (stderr, "%s\n", shell_input_line);
  1135.     }
  1136.       else
  1137.     {
  1138.       shell_input_line_size = 0;
  1139.       prompt_string_pointer = ¤t_prompt_string;
  1140.       prompt_again ();
  1141.       goto restart_read;
  1142.     }
  1143.  
  1144.       /* Add the newline to the end of this string, iff the string does
  1145.      not already end in an EOF character.  */
  1146.       if (shell_input_line_terminator != EOF)
  1147.     {
  1148.       l = shell_input_line_len;    /* was a call to strlen */
  1149.  
  1150.       if (l + 3 > shell_input_line_size)
  1151.         shell_input_line = (char *)xrealloc (shell_input_line,
  1152.                     1 + (shell_input_line_size += 2));
  1153.  
  1154.       strcpy (shell_input_line + l, "\n");
  1155.     }
  1156.     }
  1157.   
  1158.   c = shell_input_line[shell_input_line_index];
  1159.  
  1160.   if (c)
  1161.     shell_input_line_index++;
  1162.  
  1163.   if (c == '\\' && remove_quoted_newline &&
  1164.       shell_input_line[shell_input_line_index] == '\n')
  1165.     {
  1166.     prompt_again ();
  1167.     goto restart_read_next_line;
  1168.     }
  1169.  
  1170. #if defined (ALIAS)
  1171.   /*
  1172.    * If c is NULL, we have reached the end of the current input string.  If
  1173.    * pushed_string_list is non-empty, it's time to pop to the previous string
  1174.    * because we have fully consumed the result of the last alias expansion.
  1175.    * Do it transparently; just return the next character of the string popped
  1176.    * to.  We need to hang onto current_token_being_expanded until the token
  1177.    * currently being read has been recognized; we can't restore it in
  1178.    * pop_string () because the token currently being read would be
  1179.    * inappropriately compared with it.  We defer restoration until the next
  1180.    * call to read_token ().
  1181.    */
  1182.  
  1183.   if (!c && (pushed_string_list != (STRING_SAVER *)NULL))
  1184.     {
  1185.       pop_string ();
  1186.       c = shell_input_line[shell_input_line_index];
  1187.       if (c)
  1188.     shell_input_line_index++;
  1189.     }
  1190. #endif /* ALIAS */
  1191.  
  1192.   if (!c && shell_input_line_terminator == EOF)
  1193.     {
  1194.       if (shell_input_line_index != 0)
  1195.     return ('\n');
  1196.       else
  1197.     return (EOF);
  1198.     }
  1199.  
  1200.   return (c);
  1201. }
  1202.  
  1203. /* Put C back into the input for the shell. */
  1204. shell_ungetc (c)
  1205.      int c;
  1206. {
  1207.   if (shell_input_line && shell_input_line_index)
  1208.     shell_input_line[--shell_input_line_index] = c;
  1209. }
  1210.  
  1211. /* Discard input until CHARACTER is seen. */
  1212. discard_until (character)
  1213.      int character;
  1214. {
  1215.   int c;
  1216.   while ((c = shell_getc (0)) != EOF && c != character)
  1217.     ;
  1218.   if (c != EOF )
  1219.     shell_ungetc (c);
  1220. }
  1221.  
  1222. #if defined (HISTORY_REDITING)
  1223. /* Tell readline () that we have some text for it to edit. */
  1224. re_edit (text)
  1225.      char *text;
  1226. {
  1227. #if defined (READLINE)
  1228.   if (strcmp (stream_name, "readline stdin") == 0)
  1229.     bash_re_edit (text);
  1230. #endif /* READLINE */
  1231. }
  1232. #endif /* HISTORY_REDITING */
  1233.  
  1234. /* Non-zero means do no history expansion on this line, regardless
  1235.    of what history_expansion says. */
  1236. int history_expansion_inhibited = 0;
  1237.  
  1238. /* Do pre-processing on LINE.  If PRINT_CHANGES is non-zero, then
  1239.    print the results of expanding the line if there were any changes.
  1240.    If there is an error, return NULL, otherwise the expanded line is
  1241.    returned.  If ADDIT is non-zero the line is added to the history
  1242.    list after history expansion.  ADDIT is just a suggestion;
  1243.    REMEMBER_ON_HISTORY can veto, and does.
  1244.    Right now this does history expansion. */
  1245. char *
  1246. pre_process_line (line, print_changes, addit)
  1247.      char *line;
  1248.      int print_changes, addit;
  1249. {
  1250.   char *return_value = line;
  1251.   int expanded = 0;
  1252.  
  1253.   extern int history_expansion;
  1254.   extern int remember_on_history;
  1255.   int history_expand ();
  1256.   char *history_value;
  1257.  
  1258.   /* History expand the line.  If this results in no errors, then
  1259.      add that line to the history if ADDIT is non-zero. */
  1260.   if (!history_expansion_inhibited && history_expansion)
  1261.     {
  1262.       expanded = history_expand (line, &history_value);
  1263.  
  1264.       if (expanded)
  1265.     {
  1266.       if (print_changes)
  1267.         fprintf (stderr, "%s\n", history_value);
  1268.  
  1269.       /* If there was an error, return NULL. */
  1270.       if (expanded < 0)
  1271.         {
  1272.           free (history_value);
  1273.  
  1274. #if defined (HISTORY_REDITING)
  1275.           /* New hack.  We can allow the user to edit the
  1276.          failed history expansion. */
  1277.           re_edit (line);
  1278. #endif /* HISTORY_REDITING */
  1279.           return ((char *)NULL);
  1280.         }
  1281.     }
  1282.  
  1283.       /* Let other expansions know that return_value can be free'ed,
  1284.      and that a line has been added to the history list.  Note
  1285.      that we only add lines that have something in them. */
  1286.       expanded = 1;
  1287.       return_value = history_value;
  1288.     }
  1289.  
  1290.   if (addit && remember_on_history && *return_value)
  1291.     {
  1292.       extern int history_control;
  1293.       extern int history_lines_this_session;
  1294.  
  1295.       switch (history_control)
  1296.     {
  1297.       case 0:
  1298.         add_history (return_value);
  1299.         history_lines_this_session++;
  1300.         break;
  1301.       case 1:
  1302.         if (*return_value != ' ')
  1303.           {
  1304.         add_history (return_value);
  1305.         history_lines_this_session++;
  1306.           }
  1307.         break;
  1308.       case 2:
  1309.         {
  1310.           HIST_ENTRY *temp;
  1311.  
  1312.           using_history ();
  1313.           temp = previous_history ();
  1314.           if (!temp || (strcmp (temp->line, return_value) != 0))
  1315.         {
  1316.           add_history (return_value);
  1317.           history_lines_this_session++;
  1318.         }
  1319.           using_history ();
  1320.         }
  1321.         break;
  1322.     }
  1323.     }
  1324.  
  1325.   if (!expanded)
  1326.     return_value = savestring (line);
  1327.  
  1328.   return (return_value);
  1329. }
  1330.  
  1331.  
  1332. /* Place to remember the token.  We try to keep the buffer
  1333.    at a reasonable size, but it can grow. */
  1334. char *token = (char *)NULL;
  1335.  
  1336. /* Current size of the token buffer. */
  1337. int token_buffer_size = 0;
  1338.  
  1339. /* Command to read_token () explaining what we want it to do. */
  1340. #define READ 0
  1341. #define RESET 1
  1342. #define prompt_is_ps1 \
  1343.       (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
  1344.  
  1345. /* Function for yyparse to call.  yylex keeps track of
  1346.    the last two tokens read, and calls read_token.  */
  1347.  
  1348. yylex ()
  1349. {
  1350.   if (interactive && (!current_token || current_token == '\n'))
  1351.     {
  1352.       /* Before we print a prompt, we might have to check mailboxes.
  1353.      We do this only if it is time to do so. Notice that only here
  1354.      is the mail alarm reset; nothing takes place in check_mail ()
  1355.      except the checking of mail.  Please don't change this. */
  1356.       if (prompt_is_ps1 && time_to_check_mail ())
  1357.     {
  1358.       check_mail ();
  1359.       reset_mail_timer ();
  1360.     }
  1361.  
  1362.       /* Allow the execution of a random command just before the printing
  1363.      of each primary prompt.  If the shell variable PROMPT_COMMAND
  1364.      is set then the value of it is the command to execute. */
  1365.       if (prompt_is_ps1)
  1366.     {
  1367.       char *command_to_execute = get_string_value ("PROMPT_COMMAND");
  1368.  
  1369.       if (command_to_execute)
  1370.         {
  1371.           extern Function *last_shell_builtin, *this_shell_builtin;
  1372.           extern int last_command_exit_value;
  1373.           Function *temp_last, *temp_this;
  1374.           int temp_exit_value, temp_eof_encountered;
  1375.  
  1376.           temp_last = last_shell_builtin;
  1377.           temp_this = this_shell_builtin;
  1378.           temp_exit_value = last_command_exit_value;
  1379.           temp_eof_encountered = eof_encountered;
  1380.  
  1381.           parse_and_execute
  1382.         (savestring (command_to_execute), "PROMPT_COMMAND");
  1383.  
  1384.           last_shell_builtin = temp_last;
  1385.           this_shell_builtin = temp_this;
  1386.           last_command_exit_value = temp_exit_value;
  1387.           eof_encountered = temp_eof_encountered;
  1388.         }
  1389.     }
  1390.       prompt_again ();
  1391.     }
  1392.  
  1393.   token_before_that = last_read_token;
  1394.   last_read_token = current_token;
  1395.   current_token = read_token (READ);
  1396.   return (current_token);
  1397. }
  1398.  
  1399. /* Called from shell.c when Control-C is typed at top level.  Or
  1400.    by the error rule at top level. */
  1401. reset_parser ()
  1402. {
  1403.   read_token (RESET);
  1404. }
  1405.   
  1406. /* When non-zero, we have read the required tokens
  1407.    which allow ESAC to be the next one read. */
  1408. static int allow_esac_as_next = 0;
  1409.  
  1410. /* When non-zero, accept single '{' as a token itself. */
  1411. static int allow_open_brace = 0;
  1412.  
  1413. /* DELIMITER is the value of the delimiter that is currently
  1414.    enclosing, or zero for none. */
  1415. static int delimiter = 0;
  1416. static int old_delimiter = 0;
  1417.  
  1418. /* When non-zero, an open-brace used to create a group is awaiting a close
  1419.    brace partner. */
  1420. static int open_brace_awaiting_satisfaction = 0;
  1421.  
  1422. /* If non-zero, it is the token that we want read_token to return regardless
  1423.    of what text is (or isn't) present to be read.  read_token resets this. */
  1424. int token_to_read = 0;
  1425.  
  1426. /* Read the next token.  Command can be READ (normal operation) or 
  1427.    RESET (to normalize state). */
  1428. read_token (command)
  1429.      int command;
  1430. {
  1431.   extern int interactive_shell;    /* Whether the current shell is interactive. */
  1432.   int character;        /* Current character. */
  1433.   int peek_char;        /* Temporary look-ahead character. */
  1434.   int result;            /* The thing to return. */
  1435.   WORD_DESC *the_word;        /* The value for YYLVAL when a WORD is read. */
  1436.  
  1437.   if (token_buffer_size < TOKEN_DEFAULT_GROW_SIZE)
  1438.     {
  1439.       if (token)
  1440.     free (token);
  1441.       token = (char *)xmalloc (token_buffer_size = TOKEN_DEFAULT_GROW_SIZE);
  1442.     }
  1443.  
  1444.   if (command == RESET)
  1445.     {
  1446.       delimiter = old_delimiter = 0;
  1447.       open_brace_awaiting_satisfaction = 0;
  1448.       in_case_pattern_list = 0;
  1449.  
  1450. #if defined (ALIAS)
  1451.       if (pushed_string_list)
  1452.     {
  1453.       free_string_list ();
  1454.       pushed_string_list = (STRING_SAVER *)NULL;
  1455.     }
  1456.       if (pending_token_being_expanded)
  1457.     {
  1458.       free (pending_token_being_expanded);
  1459.       pending_token_being_expanded = (char *)NULL;
  1460.     }
  1461.       if (current_token_being_expanded)
  1462.     {
  1463.       free (current_token_being_expanded);
  1464.       current_token_being_expanded = (char *)NULL;
  1465.     }
  1466.  
  1467.       if (expanded_token_stack)
  1468.     {
  1469.       free_expansion_stack ();
  1470.       expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1471.     }
  1472. #endif /* ALIAS */
  1473.  
  1474.       if (shell_input_line)
  1475.     {
  1476.       free (shell_input_line);
  1477.       shell_input_line = (char *)NULL;
  1478.       shell_input_line_size = shell_input_line_index = 0;
  1479.     }
  1480.       last_read_token = '\n';
  1481.       token_to_read = '\n';
  1482.       return;
  1483.     }
  1484.  
  1485.   if (token_to_read)
  1486.     {
  1487.       int rt = token_to_read;
  1488.       token_to_read = 0;
  1489.       return (rt);
  1490.     }
  1491.  
  1492. #if defined (ALIAS)
  1493.   /*
  1494.    * Now we can replace current_token_being_expanded with 
  1495.    * pending_token_being_expanded, since the token that would be 
  1496.    * inappropriately compared has already been returned.
  1497.    *
  1498.    * To see why restoring current_token_being_expanded in pop_string ()
  1499.    * could be a problem, consider "alias foo=foo".  Then try to
  1500.    * expand `foo'.  The initial value of current_token_being_expanded is
  1501.    * NULL, so that is what is pushed onto pushed_string_list as the
  1502.    * value of saved_token_being_expanded.  "foo" then becomes shell_input_line.
  1503.    * read_token calls shell_getc for `f', `o', `o', and then shell_getc
  1504.    * hits the end of shell_input_line.  pushed_string_list is not empty
  1505.    * so it gets popped.  If we were to blindly restore
  1506.    * current_token_being_expanded at this point, `foo' would be compared
  1507.    * with a NULL string in the check for recursive expansion, and would
  1508.    * infinitely recurse.
  1509.    */
  1510.   if (pending_token_being_expanded)
  1511.     {
  1512.       if (current_token_being_expanded)
  1513.     free (current_token_being_expanded);
  1514.       current_token_being_expanded = pending_token_being_expanded;
  1515.       pending_token_being_expanded = (char *)NULL;
  1516.     }
  1517.  
  1518.   /* If we hit read_token () and there are no saved strings on the
  1519.      pushed_string_list, then we are no longer currently expanding a
  1520.      token.  This can't be done in pop_stream, because pop_stream
  1521.      may pop the stream before the current token has finished being
  1522.      completely expanded (consider what happens when we alias foo to foo,
  1523.      and then try to expand it). */
  1524.   if (!pushed_string_list && current_token_being_expanded)
  1525.     {
  1526.       free (current_token_being_expanded);
  1527.       current_token_being_expanded = (char *)NULL;
  1528.  
  1529.       if (expanded_token_stack)
  1530.     {
  1531.       free_expansion_stack ();
  1532.       expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1533.     }
  1534.     }
  1535.  
  1536.   /* This is a place to jump back to once we have successfully expanded a
  1537.      token with an alias and pushed the string with push_string () */
  1538. re_read_token:
  1539.  
  1540. #endif /* ALIAS */
  1541.  
  1542.   /* Read a single word from input.  Start by skipping blanks. */
  1543.   while ((character = shell_getc (1)) != EOF && whitespace (character));
  1544.  
  1545.   if (character == EOF)
  1546.     return (yacc_EOF);
  1547.  
  1548.   if (character == '#' && !interactive)
  1549.     {
  1550.       /* A comment.  Discard until EOL or EOF, and then return a newline. */
  1551.       discard_until ('\n');
  1552.       shell_getc (0);
  1553.  
  1554.       /* If we're about to return an unquoted newline, we can go and collect
  1555.      the text of any pending here document. */
  1556.       if (need_here_doc)
  1557.     make_here_document (redirection_needing_here_doc);
  1558.       need_here_doc = 0;
  1559.  
  1560.       return ('\n');
  1561.     }
  1562.  
  1563.   if (character == '\n')
  1564.     {
  1565.       /* If we're about to return an unquoted newline, we can go and collect
  1566.      the text of any pending here document. */
  1567.       if (need_here_doc)
  1568.     make_here_document (redirection_needing_here_doc);
  1569.       need_here_doc = 0;
  1570.  
  1571.       return (character);
  1572.     }
  1573.  
  1574.   if (member (character, "()<>;&|"))
  1575.     {
  1576.       /* Please note that the shell does not allow whitespace to
  1577.      appear in between tokens which are character pairs, such as
  1578.      "<<" or ">>".  I believe this is the correct behaviour. */
  1579.  
  1580.       if (character == (peek_char = shell_getc (1)))
  1581.     {
  1582.       switch (character)
  1583.         {
  1584.           /* If '<' then we could be at "<<" or at "<<-".  We have to
  1585.          look ahead one more character. */
  1586.         case '<':
  1587.           peek_char = shell_getc (1);
  1588.           if (peek_char == '-')
  1589.         return (LESS_LESS_MINUS);
  1590.           else
  1591.         {
  1592.           shell_ungetc (peek_char);
  1593.           return (LESS_LESS);
  1594.         }
  1595.  
  1596.         case '>': return (GREATER_GREATER);
  1597.         case ';':
  1598.               in_case_pattern_list = 1;
  1599.               return (SEMI_SEMI);
  1600.         case '&': return (AND_AND);
  1601.         case '|': return (OR_OR);
  1602.         }
  1603.     }
  1604.       else
  1605.     {
  1606.       if (peek_char == '&')
  1607.         {
  1608.           switch (character)
  1609.         {
  1610.         case '<': return (LESS_AND);
  1611.         case '>': return (GREATER_AND);
  1612.         }
  1613.         }
  1614.       if (character == '<' && peek_char == '>')
  1615.         return (LESS_GREATER);
  1616.       if (character == '>' && peek_char == '|')
  1617.         return (GREATER_BAR);
  1618.       if (peek_char == '>' && character == '&')
  1619.         return (AND_GREATER);
  1620.     }
  1621.       shell_ungetc (peek_char);
  1622.  
  1623.       /* If we look like we are reading the start of a function
  1624.      definition, then let the reader know about it so that
  1625.      we will do the right thing with `{'. */
  1626.       if (character == ')' &&
  1627.       last_read_token == '(' && token_before_that == WORD)
  1628.     allow_open_brace = 1;
  1629.  
  1630.       if (in_case_pattern_list && (character == ')'))
  1631.     in_case_pattern_list = 0;
  1632.  
  1633.       return (character);
  1634.     }
  1635.  
  1636.   /* Hack <&- (close stdin) case. */
  1637.   if (character == '-')
  1638.     {
  1639.       switch (last_read_token)
  1640.     {
  1641.     case LESS_AND:
  1642.     case GREATER_AND:
  1643.       return (character);
  1644.     }
  1645.     }
  1646.   
  1647.   /* Okay, if we got this far, we have to read a word.  Read one,
  1648.      and then check it against the known ones. */
  1649.   {
  1650.     /* Index into the token that we are building. */
  1651.     int token_index = 0;
  1652.  
  1653.     /* ALL_DIGITS becomes zero when we see a non-digit. */
  1654.     int all_digits = digit (character);
  1655.  
  1656.     /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
  1657.     int dollar_present = 0;
  1658.  
  1659.     /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
  1660.     int quoted = 0;
  1661.  
  1662.     /* Non-zero means to ignore the value of the next character, and just
  1663.        to add it no matter what. */
  1664.     int pass_next_character = 0;
  1665.  
  1666.     /* Non-zero means parsing a dollar-paren construct.  It is the count of
  1667.        un-quoted closes we need to see. */
  1668.     int dollar_paren_level = 0;
  1669.  
  1670.     /* Non-zero means parsing a dollar-bracket construct ($[...]).  It is
  1671.        the count of un-quoted `]' characters we need to see. */
  1672.     int dollar_bracket_level = 0;
  1673.  
  1674.     /* Another level variable.  This one is for dollar_parens inside of
  1675.        double-quotes. */
  1676.     int delimited_paren_level = 0;
  1677.  
  1678.     for (;;)
  1679.       {
  1680.     if (character == EOF)
  1681.       goto got_token;
  1682.  
  1683.     if (pass_next_character)
  1684.       {
  1685.         pass_next_character = 0;
  1686.         goto got_character;
  1687.       }
  1688.  
  1689.       if (delimiter && character == '\\' && delimiter != '\'')
  1690.     {
  1691.       peek_char = shell_getc (0);
  1692.       if (peek_char != '\\')
  1693.         shell_ungetc (peek_char);
  1694.       else
  1695.         {
  1696.           token[token_index++] = character;
  1697.           goto got_character;
  1698.         }
  1699.     }
  1700.  
  1701.     /* Handle backslashes.  Quote lots of things when not inside of
  1702.        double-quotes, quote some things inside of double-quotes. */
  1703.        
  1704.     if (character == '\\' && delimiter != '\'')
  1705.       {
  1706.         peek_char = shell_getc (0);
  1707.  
  1708.         /* Backslash-newline is ignored in all cases excepting
  1709.            when quoted with single quotes. */
  1710.         if (peek_char == '\n')
  1711.           {
  1712.         character = '\n';
  1713.         goto next_character;
  1714.           }
  1715.         else
  1716.           {
  1717.         shell_ungetc (peek_char);
  1718.  
  1719.         /* If the next character is to be quoted, do it now. */
  1720.         if (!delimiter || delimiter == '`' ||
  1721.             ((delimiter == '"' ) &&
  1722.              (member (peek_char, slashify_in_quotes))))
  1723.           {
  1724.             pass_next_character++;
  1725.             quoted = 1;
  1726.             goto got_character;
  1727.           }
  1728.           }
  1729.       }
  1730.  
  1731.     /* This is a hack, in its present form.  If a backquote substitution
  1732.        appears within double quotes, everything within the backquotes
  1733.        should be read as part of a single word.  Jesus.  Now I see why
  1734.        Korn introduced the $() form. */
  1735.     if (delimiter && delimiter == '"' && character == '`')
  1736.       {
  1737.         old_delimiter = delimiter;
  1738.         delimiter = character;
  1739.         goto got_character;
  1740.       }
  1741.  
  1742.     if (delimiter)
  1743.       {
  1744.         if (character == delimiter)
  1745.           {
  1746.         if (delimited_paren_level)
  1747.           {
  1748. #if defined (NOTDEF)
  1749.             report_error ("Expected ')' before %c", character);
  1750.             return ('\n');
  1751. #else
  1752.             goto got_character;
  1753. #endif /* NOTDEF */
  1754.           }
  1755.  
  1756.         delimiter = 0;
  1757.  
  1758.         if (old_delimiter == '"' && character == '`')
  1759.           {
  1760.             delimiter = old_delimiter;
  1761.             old_delimiter = 0;
  1762.           }
  1763.  
  1764.         goto got_character;
  1765.           }
  1766.       }
  1767.  
  1768.     if (!delimiter || delimiter == '`' || delimiter == '"')
  1769.       {
  1770.         if (character == '$')
  1771.           {
  1772.         peek_char = shell_getc (1);
  1773.         shell_ungetc (peek_char);
  1774.         if (peek_char == '(')
  1775.           {
  1776.             if (!delimiter)
  1777.               dollar_paren_level++;
  1778.             else
  1779.               delimited_paren_level++;
  1780.  
  1781.             pass_next_character++;
  1782.             goto got_character;
  1783.           }
  1784.         else if (peek_char == '[')
  1785.           {
  1786.             if (!delimiter)
  1787.               dollar_bracket_level++;
  1788.  
  1789.             pass_next_character++;
  1790.             goto got_character;
  1791.           }
  1792.           }
  1793.  
  1794.         /* If we are parsing a $() or $[] construct, we need to balance
  1795.            parens and brackets inside the construct.  This whole function
  1796.            could use a rewrite. */
  1797.         if (character == '(')
  1798.           {
  1799.         if (delimiter && delimited_paren_level)
  1800.           delimited_paren_level++;
  1801.  
  1802.         if (!delimiter && dollar_paren_level)
  1803.           dollar_paren_level++;
  1804.           }
  1805.  
  1806.         if (character == '[')
  1807.           {
  1808.         if (!delimiter && dollar_bracket_level)
  1809.           dollar_bracket_level++;
  1810.           }
  1811.  
  1812.         /* This code needs to take into account whether we are inside a
  1813.            case statement pattern list, and whether this paren is supposed
  1814.            to terminate it (hey, it could happen).  It's not as simple
  1815.            as just using in_case_pattern_list, because we're not parsing
  1816.            anything while we're reading a $( ) construct.  Maybe we
  1817.            should move that whole mess into the yacc parser. */
  1818.         if (character == ')')
  1819.           {
  1820.         if (delimiter && delimited_paren_level)
  1821.           delimited_paren_level--;
  1822.  
  1823.         if (!delimiter && dollar_paren_level)
  1824.           {
  1825.             dollar_paren_level--;
  1826.             goto got_character;
  1827.           }
  1828.           }
  1829.  
  1830.         if (character == ']')
  1831.           {
  1832.         if (!delimiter && dollar_bracket_level)
  1833.           {
  1834.             dollar_bracket_level--;
  1835.             goto got_character;
  1836.           }
  1837.           }
  1838.       }
  1839.  
  1840.     if (!dollar_paren_level && !dollar_bracket_level && !delimiter &&
  1841.         member (character, " \t\n;&()|<>"))
  1842.       {
  1843.         shell_ungetc (character);
  1844.         goto got_token;
  1845.       }
  1846.     
  1847.     if (!delimiter)
  1848.       {
  1849.         if (character == '"' || character == '`' || character == '\'')
  1850.           {
  1851.         quoted = 1;
  1852.         delimiter = character;
  1853.         goto got_character;
  1854.           }
  1855.       }
  1856.  
  1857.     if (all_digits) all_digits = digit (character);
  1858.     if (character == '$') dollar_present = 1;
  1859.  
  1860.       got_character:
  1861.  
  1862.     token[token_index++] = character;
  1863.  
  1864.     if (token_index == (token_buffer_size - 1))
  1865.       token = (char *)xrealloc (token, (token_buffer_size
  1866.                         += TOKEN_DEFAULT_GROW_SIZE));
  1867.     {
  1868.       char *decode_prompt_string ();
  1869.  
  1870.     next_character:
  1871.       if (character == '\n' && interactive && yy_input_type != st_string)
  1872.         prompt_again ();
  1873.     }
  1874.     /* We want to remove quoted newlines (that is, a \<newline> pair)
  1875.        unless we are within single quotes or pass_next_character is
  1876.        set (the shell equivalent of literal-next). */
  1877.     character = shell_getc ((delimiter != '\'') && (!pass_next_character));
  1878.       }
  1879.  
  1880.   got_token:
  1881.  
  1882.     token[token_index] = '\0';
  1883.     
  1884.     if ((delimiter || dollar_paren_level || dollar_bracket_level) && character == EOF)
  1885.       {
  1886.     if (dollar_paren_level && !delimiter)
  1887.       delimiter = ')';
  1888.     else if (dollar_bracket_level && !delimiter)
  1889.       delimiter = ']';
  1890.  
  1891.     report_error ("Unexpected EOF.  Looking for `%c'.", delimiter);
  1892.     return (-1);
  1893.       }
  1894.  
  1895.     if (all_digits)
  1896.       {
  1897.     /* Check to see what thing we should return.  If the last_read_token
  1898.        is a `<', or a `&', or the character which ended this token is
  1899.        a '>' or '<', then, and ONLY then, is this input token a NUMBER.
  1900.        Otherwise, it is just a word, and should be returned as such. */
  1901.  
  1902.     if ((character == '<' || character == '>') ||
  1903.         (last_read_token == LESS_AND ||
  1904.          last_read_token == GREATER_AND))
  1905.       {
  1906.         yylval.number = atoi (token); /* was sscanf (token, "%d", &(yylval.number)); */
  1907.         return (NUMBER);
  1908.       }
  1909.       }
  1910.  
  1911.     /* Handle special case.  IN is recognized if the last token
  1912.        was WORD and the token before that was FOR or CASE. */
  1913.     if ((last_read_token == WORD) &&
  1914.     ((token_before_that == FOR) || (token_before_that == CASE)) &&
  1915.     (STREQ (token, "in")))
  1916.       {
  1917.     if (token_before_that == CASE)
  1918.       {
  1919.         in_case_pattern_list = 1;
  1920.         allow_esac_as_next++;
  1921.       }
  1922.     return (IN);
  1923.       }
  1924.  
  1925.     /* Ditto for DO in the FOR case. */
  1926.     if ((last_read_token == WORD) && (token_before_that == FOR) &&
  1927.     (STREQ (token, "do")))
  1928.       return (DO);
  1929.  
  1930.     /* Ditto for ESAC in the CASE case. 
  1931.        Specifically, this handles "case word in esac", which is a legal
  1932.        construct, certainly because someone will pass an empty arg to the
  1933.        case construct, and we don't want it to barf.  Of course, we should
  1934.        insist that the case construct has at least one pattern in it, but
  1935.        the designers disagree. */
  1936.     if (allow_esac_as_next)
  1937.       {
  1938.     allow_esac_as_next--;
  1939.     if (STREQ (token, "esac"))
  1940.       {
  1941.         in_case_pattern_list = 0;
  1942.         return (ESAC);
  1943.       }
  1944.       }
  1945.  
  1946.     /* Ditto for `{' in the FUNCTION case. */
  1947.     if (allow_open_brace)
  1948.       {
  1949.     allow_open_brace = 0;
  1950.     if (STREQ (token, "{"))
  1951.       {
  1952.         open_brace_awaiting_satisfaction++;
  1953.         return ('{');
  1954.       }
  1955.       }
  1956.  
  1957. #ifdef ALIAS
  1958.     /* OK, we have a token.  Let's try to alias expand it, if (and only if)
  1959.        it's eligible. 
  1960.  
  1961.        Criteria:
  1962.         No dollar sign
  1963.         Unquoted
  1964.         Not all digits
  1965.         An identifier (all alphanumeric or underscore, first character
  1966.         is not a digit)
  1967.         Last token read was a command separator or expand_next_alias
  1968.         is set
  1969.         We are currently processing an alias (pushed_string_list is
  1970.         non-empty) and this token is not the same as the alias we're
  1971.         currently processing (or any alias we have previously
  1972.         processed.
  1973.  
  1974.        Special cases that disqualify:
  1975.         In a pattern list in a case statement (in_case_pattern_list)
  1976.     */
  1977.     if (interactive_shell && !dollar_present && !quoted &&
  1978.     !all_digits && !in_case_pattern_list &&
  1979.     (command_token_position (last_read_token) || expand_next_token))
  1980.       {
  1981.     char *alias_expand_word (), *expanded;
  1982.     if (current_token_being_expanded &&
  1983.          ((STREQ (token, current_token_being_expanded)) ||
  1984.           (token_has_been_expanded (token))))
  1985.       goto no_expansion;
  1986.  
  1987.     expanded = alias_expand_word (token);
  1988.     if (expanded)
  1989.       {
  1990.         int len = strlen (expanded), expand_next;
  1991.         char *temp;
  1992.  
  1993.         token_index = 0;    /* effectively erase current token */
  1994.  
  1995.         expand_next = (expanded[len - 1] == ' ') ||
  1996.               (expanded[len - 1] == '\t');
  1997.  
  1998.         temp = savestring (token);
  1999.         push_string (expanded, expand_next, temp);
  2000.         goto re_read_token;
  2001.       }
  2002.     else
  2003.       /* This is an eligible token that does not have an expansion. */
  2004. no_expansion:
  2005.       expand_next_token = 0;
  2006.       }
  2007.     else
  2008.       {
  2009.     expand_next_token = 0;
  2010.       }
  2011. #endif /* ALIAS */
  2012.  
  2013.     /* Check to see if it is a reserved word.  */
  2014.     if (!dollar_present && !quoted &&
  2015.     reserved_word_acceptable (last_read_token))
  2016.       {
  2017.     int i;
  2018.     for (i = 0; token_word_alist[i].word != (char *)NULL; i++)
  2019.       if (STREQ (token, token_word_alist[i].word))
  2020.         {
  2021.           if (token_word_alist[i].token == '{')
  2022.         open_brace_awaiting_satisfaction++;
  2023.  
  2024.           if (in_case_pattern_list && (token_word_alist[i].token != ESAC))
  2025.         break;
  2026.  
  2027.           if (token_word_alist[i].token == ESAC)
  2028.         in_case_pattern_list = 0;
  2029.  
  2030.           return (token_word_alist[i].token);
  2031.         }
  2032.       }
  2033.  
  2034.     /* What if we are attempting to satisfy an open-brace grouper? */
  2035.     if (open_brace_awaiting_satisfaction && strcmp (token, "}") == 0)
  2036.       {
  2037.     open_brace_awaiting_satisfaction--;
  2038.     return ('}');
  2039.       }
  2040.  
  2041.     the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
  2042.     the_word->word = (char *)xmalloc (1 + strlen (token));
  2043.     strcpy (the_word->word, token);
  2044.     the_word->dollar_present = dollar_present;
  2045.     the_word->quoted = quoted;
  2046.     the_word->assignment = assignment (token);
  2047.  
  2048.     yylval.word = the_word;
  2049.     result = WORD;
  2050.     if (last_read_token == FUNCTION)
  2051.       allow_open_brace = 1;
  2052.   }
  2053.   return (result);
  2054. }
  2055.  
  2056. #if defined (NOTDEF)        /* Obsoleted function no longer used. */
  2057. /* Return 1 if this token is a legal shell `identifier'; that is, it consists
  2058.    solely of letters, digits, and underscores, and does not begin with a 
  2059.    digit. */
  2060. legal_identifier (name)
  2061.      char *name;
  2062. {
  2063.   register char *s;
  2064.  
  2065.   if (!name || !*name)
  2066.     return (0);
  2067.  
  2068.   if (digit (*name))
  2069.     return (0);
  2070.  
  2071.   for (s = name; s && *s; s++)
  2072.     {
  2073.       if (!isletter (*s) && !digit (*s) && (*s != '_'))
  2074.     return (0);
  2075.     }
  2076.   return (1);
  2077. }
  2078. #endif /* NOTDEF */
  2079.  
  2080. #ifdef ALIAS
  2081. /* Return 1 if TOKEN is a command separating token. */
  2082. command_token_position (token)
  2083.      int token;
  2084. {
  2085.   if (member (token, "\n;(|&{") ||
  2086.       token == AND_AND ||
  2087.       token == OR_OR ||
  2088.       token == BANG ||        /* XXX */
  2089.       token == 0)
  2090.     return (1);
  2091.   else
  2092.     return (0);
  2093. }
  2094. #endif /* ALIAS */
  2095.  
  2096. /* Return 1 if TOKEN is a token that after being read would allow
  2097.    a reserved word to be seen, else 0. */
  2098. reserved_word_acceptable (token)
  2099.      int token;
  2100. {
  2101.   if (member (token, "\n;()|&{") ||
  2102.       token == AND_AND ||
  2103.       token == OR_OR ||
  2104.       token == SEMI_SEMI ||
  2105.       token == DO ||
  2106.       token == IF ||
  2107.       token == WHILE ||
  2108.       token == UNTIL ||
  2109.       token == THEN ||
  2110.       token == ELSE ||
  2111.       token == ELIF ||
  2112.       token == BANG ||
  2113.       token == 0)
  2114.     return (1);
  2115.   else
  2116.     return (0);
  2117. }
  2118.  
  2119. #if defined (READLINE)
  2120. /* Called after each time readline is called.  This insures that whatever
  2121.    the new prompt string is gets propagated to readline's local prompt
  2122.    variable. */
  2123. reset_readline_prompt ()
  2124. {
  2125.   if (prompt_string_pointer && *prompt_string_pointer)
  2126.     {
  2127.       char *temp_prompt, *decode_prompt_string ();
  2128.  
  2129.       temp_prompt = decode_prompt_string (*prompt_string_pointer);
  2130.  
  2131.       if (!temp_prompt)
  2132.     temp_prompt = savestring ("");
  2133.  
  2134.       if (current_readline_prompt)
  2135.     free (current_readline_prompt);
  2136.  
  2137.       current_readline_prompt = temp_prompt;
  2138.     }
  2139. }
  2140. #endif
  2141.  
  2142. /* Issue a prompt, or prepare to issue a prompt when the next character
  2143.    is read. */
  2144. prompt_again ()
  2145. {
  2146.   char *temp_prompt, *decode_prompt_string ();
  2147.  
  2148.   ps1_prompt = get_string_value ("PS1");
  2149.   ps2_prompt = get_string_value ("PS2");
  2150.  
  2151.   if (!prompt_string_pointer)
  2152.     prompt_string_pointer = &ps1_prompt;
  2153.  
  2154.   if (*prompt_string_pointer)
  2155.     temp_prompt = decode_prompt_string (*prompt_string_pointer);
  2156.   else
  2157.     temp_prompt = savestring ("");
  2158.  
  2159.   current_prompt_string = *prompt_string_pointer;
  2160.   prompt_string_pointer = &ps2_prompt;
  2161.  
  2162. #if defined (READLINE)
  2163.   if (!no_line_editing)
  2164.     {
  2165.       if (current_readline_prompt)
  2166.     free (current_readline_prompt);
  2167.       
  2168.       current_readline_prompt = temp_prompt;
  2169.     }
  2170.   else
  2171. #endif    /* READLINE */
  2172.     {
  2173.       if (interactive)
  2174.     {
  2175.       fprintf (stderr, "%s", temp_prompt);
  2176.       fflush (stderr);
  2177.     }
  2178.       free (temp_prompt);
  2179.     }
  2180. }
  2181.  
  2182. /* This sucks. but it is just a crock for System V systems.  The whole idea
  2183.    of MAXPATHLEN is a crock if you ask me.  Why can't we just have
  2184.    dynamically defined sizes?  (UCSB crashes every 20 minutes on me.) */
  2185. #ifndef MAXPATHLEN
  2186. #define MAXPATHLEN 1024
  2187. #endif    /* MAXPATHLEN */
  2188.  
  2189. /* Return a string which will be printed as a prompt.  The string
  2190.    may contain special characters which are decoded as follows:
  2191.    
  2192.     \t    the time
  2193.     \d    the date
  2194.     \n    CRLF
  2195.     \s    the name of the shell
  2196.     \w    the current working directory
  2197.     \W    the last element of PWD
  2198.     \u    your username
  2199.     \h    the hostname
  2200.     \#    the command number of this command
  2201.     \!    the history number of this command
  2202.     \$    a $ or a # if you are root
  2203.     \<octal> character code in octal
  2204.     \\    a backslash
  2205. */
  2206. #include <sys/param.h>
  2207. #include <time.h>
  2208.  
  2209. #define PROMPT_GROWTH 50
  2210. char *
  2211. decode_prompt_string (string)
  2212.      char *string;
  2213. {
  2214.   int result_size = PROMPT_GROWTH;
  2215.   int result_index = 0;
  2216.   char *result = (char *)xmalloc (PROMPT_GROWTH);
  2217.   int c;
  2218.   char *temp = (char *)NULL;
  2219.  
  2220.   result[0] = 0;
  2221.   while (c = *string++)
  2222.     {
  2223.       if (c == '\\')
  2224.     {
  2225.       c = *string;
  2226.  
  2227.       switch (c)
  2228.         {
  2229.  
  2230.         case '0':
  2231.         case '1':
  2232.         case '2':
  2233.         case '3':
  2234.         case '4':
  2235.         case '5':
  2236.         case '6':
  2237.         case '7':
  2238.           {
  2239.         char octal_string[4];
  2240.         int n;
  2241.  
  2242.         strncpy (octal_string, string, 3);
  2243.         octal_string[3] = '\0';
  2244.  
  2245.         n = read_octal (octal_string);
  2246.  
  2247.         temp = savestring ("\\");
  2248.         if (n != -1)
  2249.           {
  2250.             string += 3;
  2251.             temp[0] = n;
  2252.           }
  2253.  
  2254.         c = 0;
  2255.         goto add_string;
  2256.           }
  2257.       
  2258.         case 't':
  2259.         case 'd':
  2260.  
  2261.           /* Make the current time/date into a string. */
  2262.           {
  2263.         long the_time = time (0);
  2264.         char *ttemp = ctime (&the_time);
  2265.         temp = savestring (ttemp);
  2266.  
  2267.         if (c == 't')
  2268.           {
  2269.             strcpy (temp, temp + 11);
  2270.             temp[8] = '\0';
  2271.           }
  2272.         else
  2273.           temp[10] = '\0';
  2274.  
  2275.         goto add_string;
  2276.           }
  2277.  
  2278.         case 'n':
  2279.           temp = savestring ("\r\n");
  2280.           goto add_string;
  2281.  
  2282.         case 's':
  2283.           {
  2284.         extern char *shell_name;
  2285.         temp = savestring (shell_name);
  2286.         goto add_string;
  2287.           }
  2288.     
  2289.         case 'w':
  2290.         case 'W':
  2291.           {
  2292.         /* Use the value of PWD because it is much more effecient. */
  2293. #define EFFICIENT
  2294. #ifdef EFFICIENT
  2295.         char *polite_directory_format (), t_string[MAXPATHLEN];
  2296.  
  2297.         temp = get_string_value ("PWD");
  2298.  
  2299.         if (!temp)
  2300.           getwd (t_string);
  2301.         else
  2302.           strcpy (t_string, temp);
  2303. #else
  2304.         getwd (t_string);
  2305. #endif    /* EFFICIENT */
  2306.  
  2307.         if (c == 'W')
  2308.           {
  2309.             char *dir = (char *)rindex (t_string, '/');
  2310.             if (dir && dir != t_string)
  2311.               strcpy (t_string, dir + 1);
  2312.             temp = savestring (t_string);
  2313.           }
  2314.         else
  2315.           temp = savestring (polite_directory_format (t_string));
  2316.         goto add_string;
  2317.           }
  2318.       
  2319.         case 'u':
  2320.           {
  2321.         extern char *current_user_name;
  2322.         temp = savestring (current_user_name);
  2323.  
  2324.         goto add_string;
  2325.           }
  2326.  
  2327.         case 'h':
  2328.           {
  2329.         extern char *current_host_name;
  2330.         char *t_string;
  2331.  
  2332.         temp = savestring (current_host_name);
  2333.         if (t_string = (char *)index (temp, '.'))
  2334.           *t_string = '\0';
  2335.         
  2336.         goto add_string;
  2337.           }
  2338.  
  2339.         case '#':
  2340.           {
  2341.         extern int current_command_number;
  2342.         char number_buffer[20];
  2343.         sprintf (number_buffer, "%d", current_command_number);
  2344.         temp = savestring (number_buffer);
  2345.         goto add_string;
  2346.           }
  2347.  
  2348.         case '!':
  2349.           {
  2350.         extern int history_base, where_history ();
  2351.         char number_buffer[20];
  2352.  
  2353.         using_history ();
  2354.         if (get_string_value ("HISTSIZE"))
  2355.           sprintf (number_buffer, "%d",
  2356.                history_base + where_history ());
  2357.         else
  2358.           strcpy (number_buffer, "!");
  2359.         temp = savestring (number_buffer);
  2360.         goto add_string;
  2361.           }
  2362.  
  2363.         case '$':
  2364.           temp = savestring (geteuid () == 0 ? "#" : "$");
  2365.           goto add_string;
  2366.  
  2367.         case '\\':
  2368.           temp = savestring ("\\");
  2369.           goto add_string;
  2370.  
  2371.         default:
  2372.           temp = savestring ("\\ ");
  2373.           temp[1] = c;
  2374.  
  2375.         add_string:
  2376.           if (c)
  2377.         string++;
  2378.           result =
  2379.         (char *)sub_append_string (temp, result,
  2380.                        &result_index, &result_size);
  2381.           temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */
  2382.           result[result_index] = '\0';
  2383.           break;
  2384.         }
  2385.     }
  2386.       else
  2387.     {
  2388.       while (3 + result_index > result_size)
  2389.         result = (char *)xrealloc (result, result_size += PROMPT_GROWTH);
  2390.  
  2391.       result[result_index++] = c;
  2392.       result[result_index] = '\0';
  2393.     }
  2394.     }
  2395.  
  2396.   /* I don't really think that this is a good idea.  Do you? */
  2397.   if (!find_variable ("NO_PROMPT_VARS"))
  2398.     {
  2399.       WORD_LIST *expand_string (), *list;
  2400.       char *string_list ();
  2401.  
  2402.       list = expand_string (result, 1);
  2403.       free (result);
  2404.       result = string_list (list);
  2405.       dispose_words (list);
  2406.     }
  2407.  
  2408.   return (result);
  2409. }
  2410.  
  2411. /* Report a syntax error, and restart the parser.  Call here for fatal
  2412.    errors. */
  2413. yyerror ()
  2414. {
  2415.   report_syntax_error ((char *)NULL);
  2416.   reset_parser ();
  2417. }
  2418.  
  2419. /* Report a syntax error with line numbers, etc.
  2420.    Call here for recoverable errors.  If you have a message to print,
  2421.    then place it in MESSAGE, otherwise pass NULL and this will figure
  2422.    out an appropriate message for you. */
  2423. report_syntax_error (message)
  2424.      char *message;
  2425. {
  2426.   if (message)
  2427.     {
  2428.       if (!interactive)
  2429.     {
  2430.       char *name = stream_name ? stream_name : "stdin";
  2431.       report_error ("%s:%d: `%s'", name, line_number, message);
  2432.     }
  2433.       else
  2434.     report_error ("%s", message);
  2435.  
  2436.       return;
  2437.     }
  2438.  
  2439.   if (shell_input_line && *shell_input_line)
  2440.     {
  2441.       char *error_token, *t = shell_input_line;
  2442.       register int i = shell_input_line_index;
  2443.       int token_end = 0;
  2444.  
  2445.       if (!t[i] && i)
  2446.     i--;
  2447.  
  2448.       while (i && (t[i] == ' ' || t[i] == '\t' || t[i] == '\n'))
  2449.     i--;
  2450.  
  2451.       if (i)
  2452.     token_end = i + 1;
  2453.  
  2454.       while (i && !member (t[i], " \n\t;|&"))
  2455.     i--;
  2456.  
  2457.       while (i != token_end && member (t[i], " \n\t"))
  2458.     i++;
  2459.  
  2460.       if (token_end)
  2461.     {
  2462.       error_token = (char *)alloca (1 + (token_end - i));
  2463.       strncpy (error_token, t + i, token_end - i);
  2464.       error_token[token_end - i] = '\0';
  2465.  
  2466.       report_error ("syntax error near `%s'", error_token);
  2467.     }
  2468.       else if ((i == 0) && (token_end == 0))    /* a 1-character token */
  2469.     {
  2470.       error_token = (char *) alloca (2);
  2471.       strncpy(error_token, t + i, 1);
  2472.       error_token[1] = '\0';
  2473.  
  2474.       report_error ("syntax error near `%s'", error_token);
  2475.     }
  2476.  
  2477.       if (!interactive)
  2478.     {
  2479.       char *temp = savestring (shell_input_line);
  2480.       char *name = stream_name ? stream_name : "stdin";
  2481.       int l = strlen (temp);
  2482.  
  2483.       while (l && temp[l - 1] == '\n')
  2484.         temp[--l] = '\0';
  2485.  
  2486.       report_error ("%s:%d: `%s'", name, line_number, temp);
  2487.       free (temp);
  2488.     }
  2489.     }
  2490.   else
  2491.     report_error ("Syntax error");
  2492. }
  2493.  
  2494. /* ??? Needed function. ??? We have to be able to discard the constructs
  2495.    created during parsing.  In the case of error, we want to return
  2496.    allocated objects to the memory pool.  In the case of no error, we want
  2497.    to throw away the information about where the allocated objects live.
  2498.    (dispose_command () will actually free the command. */
  2499. discard_parser_constructs (error_p)
  2500.      int error_p;
  2501. {
  2502. /*   if (error_p) {
  2503.      fprintf (stderr, "*");
  2504.   } */
  2505. }
  2506.    
  2507. /* Do that silly `type "bye" to exit' stuff.  You know, "ignoreeof". */
  2508.  
  2509. /* The number of times that we have encountered an EOF character without
  2510.    another character intervening.  When this gets above the limit, the
  2511.    shell terminates. */
  2512. int eof_encountered = 0;
  2513.  
  2514. /* The limit for eof_encountered. */
  2515. int eof_encountered_limit = 10;
  2516.  
  2517. /* If we have EOF as the only input unit, this user wants to leave
  2518.    the shell.  If the shell is not interactive, then just leave.
  2519.    Otherwise, if ignoreeof is set, and we haven't done this the
  2520.    required number of times in a row, print a message. */
  2521. handle_eof_input_unit ()
  2522. {
  2523.   extern int login_shell, EOF_Reached;
  2524.  
  2525.   if (interactive)
  2526.     {
  2527.       /* If the user wants to "ignore" eof, then let her do so, kind of. */
  2528.       if (find_variable ("ignoreeof") || find_variable ("IGNOREEOF"))
  2529.     {
  2530.       if (eof_encountered < eof_encountered_limit)
  2531.         {
  2532.           fprintf (stderr, "Use \"%s\" to leave the shell.\n",
  2533.                login_shell ? "logout" : "exit");
  2534.           eof_encountered++;
  2535.           /* Reset the prompt string to be $PS1. */
  2536.           prompt_string_pointer = (char **)NULL;
  2537.           prompt_again ();
  2538.           last_read_token = current_token = '\n';
  2539.           return;
  2540.         } 
  2541.     }
  2542.  
  2543.       /* In this case EOF should exit the shell.  Do it now. */
  2544.       reset_parser ();
  2545.       exit_builtin ((WORD_LIST *)NULL);
  2546.     }
  2547.   else
  2548.     {
  2549.       /* We don't write history files, etc., for non-interactive shells. */
  2550.       EOF_Reached = 1;
  2551.     }
  2552. }
  2553.